package com.gzy.studentsociety.web;

import com.gzy.studentsociety.dao.StudentDao;
import com.gzy.studentsociety.dao.StudentDaoImpl;
import com.gzy.studentsociety.entity.Student;
import com.sun.xml.internal.ws.handler.HandlerException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;

/**
 * @version: java version 1.8
 * @Author: Chuan Liu
 * @description:
 * @date: 2024-10-28 16:07
 */
@WebServlet(name = "student", urlPatterns = "/student/*")
public class StudentServlet extends RouteServlet {
    private StudentDao studentDao = new StudentDaoImpl();
    public String login(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        String account = req.getParameter("account");
        String password = req.getParameter("password");
        Student stu = null;
        try {
            stu = studentDao.login(account,password);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
            handleException(throwables, req, res);
        }
        if(stu != null){
            HttpSession session = req.getSession();
            session.setAttribute("student",stu.getStudentNumber());
//            res.sendRedirect("/studentSociety_war_exploded/main.jsp");
            res.sendRedirect("/studentSociety_war_exploded/society/mainIndex");
        }else{
            req.getRequestDispatcher("/index.jsp").forward(req,res);
        }

        return "direct";
    }


    public void doGet(HttpServletRequest req, HttpServletResponse res)
            throws ServletException, IOException {
        this.doPost(req, res);
    }


    protected Class getMyClass() {
        return this.getClass();
    }
}
